/**
 * 
 */
package tests;

import static org.junit.Assert.*;
import interfaces.Board;
import interfaces.Cell;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import plugins.AliveState;
import plugins.ConwayCell;
import plugins.DeadState;
import framework.BoardImpl;
import framework.InvalidCellsArrayException;

/**
 * Tests to make sure BoardImpl constructor works properly and that
 * various Board methods work correctly
 * @author bcharna
 * 
 */

public class BoardImplTest {
	
	Cell[][] deadCells;
	final int WIDTH = 100;
	final int HEIGHT = 101;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		
		//setup some dead cells
		deadCells = new Cell[WIDTH][HEIGHT];
		// fill board with cells.
		for (int x = 0; x < WIDTH; x++)
			for (int y = 0; y < HEIGHT; y++)
			{
				deadCells[x][y] = new ConwayCell(); // how will we know what to load with?
				//cells[x][y].setBoard(this); //this needed?
				//cells[x][y].setAlgorithm(a);
				deadCells[x][y].setState(new DeadState()); //make all cells dead
			}
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}
	
	
	/**
	 * Upon creating a new board, will cells, we should see that
	 * Board's cells instance variable was properly set, with the 
	 * right dimensions.  Also, we should see that all cells are 'Dead'
	 */
	@Test
	public void newBoardTest()
	{
		// ensure Cell[][] array lengths are right
		Board b = null;
		try {
			b = new BoardImpl(deadCells, WIDTH, HEIGHT);
		} catch (InvalidCellsArrayException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals(WIDTH, b.getWidth());
		assertEquals(HEIGHT, b.getHeight());

		assertEquals(WIDTH, b.getCells().length); // should be 100 cols

		// ensure every cell on board is Dead
		for (int x = 0; x < b.getWidth(); x++)
		{
			assertEquals(HEIGHT, b.getCells()[x].length); // each col has 101 rows		
			for (int y = 0; y < b.getHeight(); y++)
			{
				assertEquals(DeadState.class, b.getCells()[x][y].getState().getClass());
				assertEquals(DeadState.class, b.getCell(x, y).getState().getClass());
			}
		}
	}
	
	
	/**
	 * After changing all the states of cells in a board,
	 * we should see that this change actually occurred.
	 */
	@Test
	public void stateChangeTest()
	{
		// ensure Cell[][] array lengths are right
		Board b = null;
		try {
			b = new BoardImpl(deadCells, WIDTH, HEIGHT);
		} catch (InvalidCellsArrayException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals(WIDTH, b.getWidth());
		assertEquals(HEIGHT, b.getHeight());

		assertEquals(WIDTH, b.getCells().length); // should be 100 cols
		for (int x = 0; x < b.getWidth(); x++)
		{
			assertEquals(HEIGHT, b.getCells()[x].length); // each col has 101 rows
			for (int y = 0; y < b.getHeight(); y++)
			{
				assertEquals(DeadState.class, b.getCells()[x][y].getState().getClass());
				assertEquals(DeadState.class, b.getCell(x, y).getState().getClass());
				Cell c = b.getCell(x, y);
				c.setState(new AliveState()); // change to alive state
			}
		}
		//ensure all are alive now.
		for (int x = 0; x < b.getWidth(); x++)
			for (int y = 0; y < b.getHeight(); y++)
			{
				assertEquals(AliveState.class, b.getCells()[x][y].getState().getClass());
				assertEquals(AliveState.class, b.getCell(x, y).getState().getClass());
			}
	}
	
}
